using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using NerdDinner.Web.Models.Repository;
using NerdDinner.Web.Models.Data;
using NerdDinner.Web.Helpers;

namespace NerdDinner.Web.Controllers
{
    [HandleElmahError]
    public class RSVPController : Controller
    {
        private IDinnerRepository _repository;

        public RSVPController()
        {
            _repository = new DinnerRepository();
        }

        // 
        // AJAX: /Dinners/RSVPForEvent/1 
        [Authorize, AcceptVerbs(HttpVerbs.Post)] 
        public ActionResult Register(int number) 
        {
            Dinner dinner = _repository.GetDinner(number); 

            if (!dinner.HasUserRsvped(User.Identity.Name)) 
            { 
                RSVP rsvp = new RSVP(); 
                rsvp.AttendeeName = User.Identity.Name; 
                dinner.RSVPs.Add(rsvp);
                _repository.Save(); 
            } 
            
            return Content("Thanks - we'll see you there!"); 
        } 
    }
}
